package com.artpri.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.artpri.model.DeliverAdd;
import com.artpri.model.GoodsOrder;
import com.artpri.model.OrderInfo;
import com.artpri.model.ShopkeeperInfo;
import com.artpri.model.vo.OrderInfoVo;
import com.artpri.service.OrderInfoService;
import com.artpri.util.TimeUtil;

/**
 * 订单 -service
 * @author ZhangSi
 */
public class OrderInfoServiceImpl extends BaseService implements OrderInfoService{

	@Override
	@Transactional
	public boolean addOrder(OrderInfo order, List<GoodsOrder> goodsOrders, int addId) {
		
		if(order.getShopkeeperNum() == null) {
			return false;
		}
		
		// 设置收件信息
		DeliverAdd address = getDeliverAddDao().get(DeliverAdd.class, addId);
		order.setOrderConsignee(address.getDaName());
		order.setOrderAddress(address.getDaRegion() + address.getDaAddress());
		order.setOrderPhone(address.getDaPhonenum());
		//设置订单提交时间
		order.setOrderCreatTime(TimeUtil.getTimeStamp());
		//设置订单过期时间
		order.setOrderPastTime(TimeUtil.getTimeStamp());
		// 设置订单状态
		order.setOrderState(1);
		// 设置支付状态
		order.setOrderPayState(0);
		//设置积分金额
		order.setOrderJf(0.0);
		//设置红包金额
		order.setOrderBonus(0.0);
		//设置运费
		order.setOrderFreight(0.0);
		//设置运费险
		order.setOrderSupportMoney(0.0);
		//设置物流状态
		order.setOrderDeliverState(0);

		Integer orderId = (Integer) getOrderInfoDao().save(order);
		if (orderId != null) {
			for (Iterator<GoodsOrder> it = goodsOrders.iterator(); it.hasNext();) {
				GoodsOrder goodsOrder = (GoodsOrder) it.next();
				goodsOrder.setOrderId(orderId);
			}
		}
		
		getGoodsOrderDao().save(goodsOrders);
		
		return true;
	}

	
	@Override
	@Transactional(readOnly=true)
	public List<OrderInfo> listOrderByPage(Integer userNum, OrderType type, int pageNo, int pageSize) {
		return getOrderInfoDao().getOrderListByPage(userNum, type, pageNo, pageSize);
	}
	
	
	@Override
	@Transactional(readOnly=true)
	public List<OrderInfoVo> listOrderVo(Integer userNum, String type) {
		OrderType orderType = getOrderType(type);
		if (orderType != null) {
			return listOrderVo(userNum, orderType);
		} else {
			return null;
		}
	}
	
	@Override
	@Transactional(readOnly=true)
	public List<OrderInfoVo> listOrderVoByPage(Integer userNum, String type, int pageNo, int pageSize) {
		OrderType orderType = getOrderType(type);
		if (orderType != null) {
			return listOrderVoByPage(userNum, orderType, pageNo, pageSize);
		} else {
			return null;
		}
	}

	@Override
	@Transactional(readOnly=true)
	public List<OrderInfoVo> listOrderVo(Integer userNum, OrderType type) {
		return listOrder(userNum, type, 0, 0, false);
	}
	
	@Override
	@Transactional(readOnly=true)
	public List<OrderInfoVo> listOrderVoByPage(Integer userNum, OrderType type,
			int pageNo, int pageSize) {
		return listOrder(userNum, type, pageNo, pageSize, true);
	}
	
	private List<OrderInfoVo> listOrder(Integer userNum, OrderType type, int pageNo, int pageSize, boolean findByPage) {
		List<OrderInfoVo> list = null;
		List<OrderInfo> orderList = null;
		
		if (findByPage) {
			orderList = getOrderInfoDao().getOrderListByPage(userNum, type, pageNo, pageSize);
		} else {
			orderList = getOrderInfoDao().getOrderList(userNum, type);
		}
		
		if (orderList != null && orderList.size() != 0) {
			list = new ArrayList<>();
			OrderInfoVo vo = null;
			for (Iterator<OrderInfo> it = orderList.iterator(); it.hasNext();) {
				OrderInfo order = (OrderInfo) it.next();
				vo = new OrderInfoVo();
				vo.setOrderInfo(order);
				vo.setShopkeeper(getShopkeeperDao().get(ShopkeeperInfo.class, order.getShopkeeperNum()));
				List<GoodsOrder> goodsOrderList = getGoodsOrderDao().findByOrder(order.getOrderId());
				vo.setGoodsOrder(goodsOrderList);
				list.add(vo);
			}
		}
		
		return list;
	}
	
	private OrderType getOrderType(String type) {
		switch (type) {
		case "all":
			return OrderType.ALL;
		case "wait_pay":
			return OrderType.WAIT_PAY;
		case "wait_comment":
			return OrderType.WAIT_COMMENT;
		case "wait_goods":
			return OrderType.WAIT_GOODS;
		case "canceled":
			return OrderType.CANCELED;
		case "done_deal":
			return OrderType.DONE_DEAL;
		case "unfinish":
			return OrderType.UNFINISH;
		default:
			return null;
		}
	}
	
	@Override
	@Transactional
	public void cancelOrder(Integer id, String cancelReason) {
		OrderInfo order = getOrderInfoDao().get(OrderInfo.class, id);

		// 0.订单状态 (0订单提交但未确认 1已确认 2已取消 3过期)
		order.setOrderState(2);
		// 1.物流状态 (0未发货 1已发货 2已取消 3配货中 5发货中 6配送完成)
		order.setOrderDeliverState(0);
		// 2.退款状态 
		order.setOrderRefundState(0);
		// 3.订单取消时间
		order.setOrderCancelTime(TimeUtil.getTimeStamp());
		// 4.订单取消原因
		order.setOrderCancelReson(cancelReason);

		getOrderInfoDao().update(order);
	}

	@Override
	@Transactional(readOnly=true)
	public List<Integer> countOrder(Integer userNum) {
		List<Integer> list = new ArrayList<>(4);
		int pay = getOrderInfoDao().getOrderCount(userNum, OrderType.WAIT_PAY);
		int comment = getOrderInfoDao().getOrderCount(userNum, OrderType.WAIT_COMMENT);
		int goods = getOrderInfoDao().getOrderCount(userNum, OrderType.WAIT_GOODS);
		list.add(pay);
		list.add(comment);
		list.add(goods);
		list.add(0);
		return list;
	}


	@Override
	@Transactional(readOnly=true)
	public Integer getOrderCount(Integer userNum, String orderType) {
		OrderType type = getOrderType(orderType);
		if (type != null) {
			return getOrderInfoDao().getOrderCount(userNum, type);
		}
		return null;
	}

	@Override
	@Transactional(readOnly=true)
	public Integer getOrderCount(Integer userNum, OrderType type) {
		return getOrderInfoDao().getOrderCount(userNum, type);
	}

}
